const router = require('koa-router')()

const db = require('../db')

router.get('/', async ctx => { 
    list = await db.isconnection().find('user', '')
    // console.log(list)
    await ctx.render('home',{
        list
    })
})

router.get('/edit', async ctx => { 
    // console.log(ctx.query.id)
    let nowdb = db.isconnection()
    let id = nowdb.getObjectId(ctx.query.id)
    let res = await nowdb.find('user', { _id: id })
    await ctx.render('edit', {
        msg:res[0]
    })
})

router.get('/delete', async ctx => { 
    let nowdb = db.isconnection()
    // console.log(ctx.query.id)
    let id = nowdb.getObjectId(ctx.query.id)
    let res = await nowdb.delete('user', { _id: id })
    if (res.n === 1) { 
        await ctx.render('response', {
            msg: '删除成功'
        })
        return
    }
    await ctx.render('response', {
        msg:'删除失败'
    })
})

router.get('/addone', async ctx => { 
    await ctx.render('addone')
})

module.exports = router